package com.xiepxcc.annotation.base.validtor;

import com.xiepxcc.annotation.base.vo.UserVO;
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import org.springframework.validation.annotation.Validated;

import java.lang.annotation.Annotation;

public class UserValidtor implements Validator {

    /**
     * 判断支持的JAVABEAN 类型
     * @param aClass
     * @return
     */
    @Override
    public boolean supports(Class<?> aClass) {
        return UserVO.class.equals(aClass);
    }

    @Override
    public void validate(Object o, Errors errors) {
        //把校验信息注册到Error的实现类里
        ValidationUtils.rejectIfEmpty(errors,"id",null,"id不能为空!");
        ValidationUtils.rejectIfEmpty(errors,"name",null,"name不能为空!");
        UserVO personScope = (UserVO) o;
        if(StringUtils.isEmpty(personScope.getPass())){
            errors.rejectValue("pass",null,"密码不能为空!!");
        }

    }
}
